package cn.iocoder.yudao.module.dify.controller.admin.riskreportrecords;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.dify.controller.admin.riskreportrecords.vo.RiskReportRecordsPageReqVO;
import cn.iocoder.yudao.module.dify.controller.admin.riskreportrecords.vo.RiskReportRecordsRespVO;
import cn.iocoder.yudao.module.dify.controller.admin.riskreportrecords.vo.RiskReportRecordsSaveReqVO;
import cn.iocoder.yudao.module.dify.dal.dataobject.riskreportrecords.RiskReportRecordsDO;
import cn.iocoder.yudao.module.dify.service.riskreportrecords.RiskReportRecordsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - dify风险管理报告任务记录")
@RestController
@RequestMapping("/dify/risk-report-records")
@Validated
public class RiskReportRecordsController {

    @Resource
    private RiskReportRecordsService riskReportRecordsService;

    @PostMapping("/create")
    @Operation(summary = "创建dify风险管理报告任务记录")
    @PreAuthorize("@ss.hasPermission('dify:risk-report-records:create')")
    public CommonResult<Long> createRiskReportRecords(@Valid @RequestBody RiskReportRecordsSaveReqVO createReqVO) {
        return success(riskReportRecordsService.createRiskReportRecords(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新dify风险管理报告任务记录")
    @PreAuthorize("@ss.hasPermission('dify:risk-report-records:update')")
    public CommonResult<Boolean> updateRiskReportRecords(@Valid @RequestBody RiskReportRecordsSaveReqVO updateReqVO) {
        riskReportRecordsService.updateRiskReportRecords(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除dify风险管理报告任务记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dify:risk-report-records:delete')")
    public CommonResult<Boolean> deleteRiskReportRecords(@RequestParam("id") Long id) {
        riskReportRecordsService.deleteRiskReportRecords(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得dify风险管理报告任务记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dify:risk-report-records:query')")
    public CommonResult<RiskReportRecordsRespVO> getRiskReportRecords(@RequestParam("id") Long id) {
        RiskReportRecordsDO riskReportRecords = riskReportRecordsService.getRiskReportRecords(id);
        return success(BeanUtils.toBean(riskReportRecords, RiskReportRecordsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得dify风险管理报告任务记录分页")
    @PreAuthorize("@ss.hasPermission('dify:risk-report-records:query')")
    public CommonResult<PageResult<RiskReportRecordsRespVO>> getRiskReportRecordsPage(@Valid RiskReportRecordsPageReqVO pageReqVO) {
        pageReqVO.setCreator(getLoginUserId().toString());
        PageResult<RiskReportRecordsDO> pageResult = riskReportRecordsService.getRiskReportRecordsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RiskReportRecordsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出dify风险管理报告任务记录 Excel")
    @PreAuthorize("@ss.hasPermission('dify:risk-report-records:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRiskReportRecordsExcel(@Valid RiskReportRecordsPageReqVO pageReqVO,
                                             HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RiskReportRecordsDO> list = riskReportRecordsService.getRiskReportRecordsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "dify风险管理报告任务记录.xls", "数据", RiskReportRecordsRespVO.class,
                BeanUtils.toBean(list, RiskReportRecordsRespVO.class));
    }

}